import com.service.impl.MessageServiceImpl;
import com.dao.ChatMessageDao;
import com.entity.ChatMessageEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
 * 验证MessageService修复的简单测试
 */
@ExtendWith(MockitoExtension.class)
public class 验证MessageService修复 {

    @Mock
    private ChatMessageDao chatMessageDao;

    @InjectMocks
    private MessageServiceImpl messageService;

    @Test
    void testSaveMessage_WithNullContent() {
        // 测试空消息内容
        boolean result = messageService.saveMessage(null, "user1", "user2");
        
        // 验证结果
        assertFalse(result);
        verify(chatMessageDao, never()).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_WithEmptyContent() {
        // 测试空字符串消息内容
        boolean result = messageService.saveMessage("", "user1", "user2");
        
        // 验证结果
        assertFalse(result);
        verify(chatMessageDao, never()).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_WithWhitespaceContent() {
        // 测试只包含空白字符的消息
        boolean result = messageService.saveMessage("   \t\n   ", "user1", "user2");
        
        // 验证结果
        assertFalse(result);
        verify(chatMessageDao, never()).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_WithNullSenderId() {
        // 测试空发送者ID
        boolean result = messageService.saveMessage("Test message", null, "user2");
        
        // 验证结果
        assertFalse(result);
        verify(chatMessageDao, never()).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_WithNullReceiverId() {
        // 测试空接收者ID
        boolean result = messageService.saveMessage("Test message", "user1", null);
        
        // 验证结果
        assertFalse(result);
        verify(chatMessageDao, never()).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_Success() {
        // 模拟数据库插入成功
        when(chatMessageDao.insert(any(ChatMessageEntity.class))).thenReturn(1);

        // 执行测试
        boolean result = messageService.saveMessage("Test message", "user1", "user2");

        // 验证结果
        assertTrue(result);
        verify(chatMessageDao).insert(any(ChatMessageEntity.class));
    }

    @Test
    void testSaveMessage_Exception() {
        // 模拟数据库异常
        when(chatMessageDao.insert(any(ChatMessageEntity.class)))
            .thenThrow(new RuntimeException("Database error"));

        // 执行测试
        boolean result = messageService.saveMessage("Test message", "user1", "user2");

        // 验证结果
        assertFalse(result);
        verify(chatMessageDao).insert(any(ChatMessageEntity.class));
    }
}
